package com.minimalist.basic.controller.basic;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.minimalist.basic.service.FileService;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.basic.vo.file.*;
import com.minimalist.common.response.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "文件管理")
@RequestMapping("/basic/file")
public class FileController{

    @Autowired
    private FileService fileService;

    @PostMapping("/uploadFile")
    @SaCheckPermission("basic:file:upload")
    @Operation(summary = "单文件上传")
    public R<FileUploadRespVO> uploadFile(@Validated FileUploadVO fileUploadVO){
        return R.ok(fileService.uploadFile(fileUploadVO));
    }

    @PostMapping("/uploadFileBatch")
    @SaCheckPermission("basic:file:upload")
    @Operation(summary = "批量文件上传")
    public R<List<FileUploadRespVO>> uploadFileBatch(
            @Validated FileUploadBatchVO uploadBatchVO){
        return R.ok(fileService.uploadFileBatch(uploadBatchVO));
    }

    @DeleteMapping("/deleteFile")
    @SaCheckPermission("basic:file:delete")
    @Operation(summary = "单文件删除")
    public R<Void> deleteFile(
            @RequestParam("fileId") @NotNull(message = "文件ID不能为空")
            @Parameter(name = "fileId", required = true, description = "fileId")
            Long fileId){
        fileService.deleteFile(fileId);
        return R.ok();
    }

    @DeleteMapping("/deleteBatchFiles")
    @SaCheckPermission("basic:file:delete")
    @Operation(summary = "多文件删除")
    public R<Void> deleteBatchFiles(
            @RequestParam("fileIds") @NotNull(message = "文件ID不能为空")
            @Parameter(name = "fileIds", required = true, description = "fileIds")
            List<Long> fileIds){
        fileService.deleteBatchFiles(fileIds);
        return R.ok();
    }

    @GetMapping("/getPageFileList")
    @SaCheckPermission("basic:file:get")
    @Operation(summary = "查询文件列表(分页)")
    public R<PageResp<FileVO>> getPageFileList(FileQueryVO queryVO){
        System.out.println("queryVO = " + queryVO);
        return R.ok(fileService.getPageFileList(queryVO));
    }

    @GetMapping("/getFileByFileId/{fileId}")
    @SaCheckPermission("basic:file:get")
    @Operation(summary = "根据文件ID查询文件信息")
    public R<FileVO> getFileByFileId(
            @PathVariable("fileId") @NotNull(message = "文件ID不能为空")
            @Parameter(name = "fileId", required = true, description = "文件ID")
            Long fileId){
        return R.ok(fileService.getFileByFileId(fileId));
    }

    @GetMapping("getFileListByFileIds")
    @SaCheckPermission("basic:file:get")
    @Operation(summary = "根据文件ID列表查询文件信息")
    public R<List<FileVO>> getFileListByFileIds(
            @RequestParam("fileIds") @NotNull(message = "文件ID列表不能为空")
            @Parameter(name = "fileIds", required = true, description = "文件ID列表")
            List<Long> fileIds){
        return R.ok(fileService.getFileListByFileIds(fileIds));
    }

    @PostMapping("/updateStatusByFileUrl")
    @SaCheckPermission("basic:file:update")
    @Operation(summary = "根据文件URL更新文件状态")
    public R<Void> updateStatusByFileUrl(
            @Validated FileUpdateStatusVO fileUpdateStatusVO){
        fileService.updateStatusByFileUrl(fileUpdateStatusVO);
        return R.ok();
    }

    @GetMapping("/downloadFile/{fileId}")
    @SaIgnore
    @Operation(summary = "文件下载")
    public void downloadFile(
            @PathVariable("fileId") @NotNull(message = "文件ID不能为空")
            @Parameter(name = "fileId", required = true, description = "文件ID")
            Long fileId, HttpServletResponse response){
        fileService.downloadFile(fileId, response);
    }
}
